//
//  ZSMyRoseViewController.m
//  ROSE
//
//  Created by Apple on 2018/6/1.
//  Copyright © 2018年 HeNanZhongDunYunAn. All rights reserved.
//

#import "ZSMyRoseViewController.h"
#import "ZSGiveViewController.h"
#import "ZSMyRoseListVC.h"
#define KIsiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)  

@interface ZSMyRoseViewController ()<UIScrollViewDelegate>
/**
 *  标签底部红色指示器
 */
@property(nonatomic,weak)UIView *indicatorView;
/**
 *  当前选中的顶部标签内部的按钮
 */
@property(nonatomic,weak)UIButton *selectedButton;

/**
 *  顶部所有标签的view
 */
@property(nonatomic,weak)UIView *titlesView;

/**
 *  内容视图
 */
@property(nonatomic,weak)UIScrollView *contentView;
@end

@implementation ZSMyRoseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的玫瑰";
    
    // 初始化所有子控制器
    [self setupChildVces];
    
    // 设置顶部的标签栏
    [self setupTitlesView];
    
    // 底部cententView
    [self setupContentView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)setupChildVces
{
    ZSMyRoseListVC *my = [[ZSMyRoseListVC alloc] init];
    [self addChildViewController:my];
    
    ZSGiveViewController *giv = [[ZSGiveViewController alloc] init];
    [self addChildViewController:giv];
}

- (void)setupContentView
{
    CGFloat headerHeight = KIsiPhoneX?44:20+44;
    
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.frame = CGRectMake(0, _titlesView.bottom, size_width, size_height - _titlesView.height - headerHeight);
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    [contentView setShowsHorizontalScrollIndicator:NO];
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    self.contentView = contentView;
    // 添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:contentView];
    
    // 不要自动调整inset
    NSString *version= [UIDevice currentDevice].systemVersion;
    if(version.doubleValue >=11.0) {
        _contentView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)setupTitlesView
{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
    titlesView.width = self.view.width;
    titlesView.height = 61.5;
    titlesView.y = 0;
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    // 底部红色指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor themeColor];
    indicatorView.height = 1.5;
    indicatorView.tag = -1;
    indicatorView.y = titlesView.height - indicatorView.height;
    self.indicatorView = indicatorView;
    
    // 内部子标签
    NSArray *titles = @[@"我的玫瑰",@"送出玫瑰"];
    CGFloat height = 31;
    CGFloat width = 60;
    
    for (NSInteger i=0; i<titles.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = 35 + i * (button.width+35);
        button.y = 29;
        [button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
//        button.backgroundColor = [UIColor RandomColor];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button layoutIfNeeded]; // 强制布局（强制更新子控件的frame）
        [button setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor themeColor] forState:UIControlStateDisabled];
        [button.titleLabel setFont:[UIFont systemFontOfSize:13.0 weight:1]];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        button.width = button.titleLabel.width;
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
            [self.selectedButton.titleLabel setFont:[UIFont systemFontOfSize:14.0 weight:5]];
            
            // 让按钮内部的label根据文字内容计算尺寸
            [button.titleLabel sizeToFit];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.indicatorView.width = button.titleLabel.width;
                self.indicatorView.centerX = button.centerX;
            });
        }
        
    }
    
    UIView *line = [[UIView alloc] initWithFrame:(CGRectMake(0, titlesView.height-0.5, size_width, 0.5))];
    line.backgroundColor = [UIColor themeColor];
    [titlesView addSubview:line];
    
    
    // indicatorView最后才添加到titlesView里
    [titlesView addSubview:indicatorView];
    // 为了后面从titlesView取button方便
    // ...
}

/**
 *  点击了标签栏里的按钮
 */
- (void)titleClick:(UIButton *)button
{
    // 修改按钮的状态
    [button.titleLabel setFont:[UIFont systemFontOfSize:14.0 weight:5]];
    button.enabled = NO;
    
    UIButton *oldButton = self.selectedButton;
    oldButton.enabled = YES;
    [oldButton.titleLabel setFont:[UIFont systemFontOfSize:13.0 weight:1]];
    
    self.selectedButton = button;
    
    // 让标签执行动画
    [UIView animateWithDuration:.025 animations:^{
        self.indicatorView.width = self.selectedButton.titleLabel.width;
        self.indicatorView.centerX = self.selectedButton.centerX;
    }];
    
    // 滚动contentView
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 当前的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    // 取出子控制器
    //    UITableViewController *vc = self.childViewControllers[index];
    //    vc.view.x = scrollView.contentOffset.x;
    //    vc.view.y = 0; //设置控制器view的y值为0（默认是20）
    //    vc.view.height = scrollView.height; //设置控制器view的height值为整个屏幕的高度（默认是比屏幕高度少20）
    //
    //    // 设置内边距
    //    CGFloat top = CGRectGetMaxY(self.titlesView.frame);
    //    CGFloat bottom = self.tabBarController.tabBar.height;
    //    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    //    // 设置滚动条的内边距
    //    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    
    UIViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0; //设置控制器view的y值为0（默认是20）
    vc.view.height = scrollView.height; //设置控制器view的height值为整个屏幕的高度（默认是比屏幕高度少20）
    
    // 设置内边距
    CGFloat top = CGRectGetMaxY(self.titlesView.frame);
    CGFloat bottom = self.tabBarController.tabBar.height;
    //        vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    //        // 设置滚动条的内边距
    //        vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击菜单按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titlesView.subviews[index]];
}

@end
